import { HttpClient } from '@angular/common/http';
import { Injectable, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { ACLService } from '@delon/acl';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import { ALAIN_I18N_TOKEN, MenuService, SettingsService, TitleService } from '@delon/theme';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzIconService } from 'ng-zorro-antd/icon';
import { Observable, zip, of, pipe } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

import { ICONS } from '../../../style-icons';
import { ICONS_AUTO } from '../../../style-icons-auto';
import { I18NService } from '../i18n/i18n.service';

/**
 * Used for application startup
 * Generally used to get the basic data of the application, like: Menu Data, User Data, etc.
 */
@Injectable()
export class StartupService {
  constructor(
    iconSrv: NzIconService,
    private menuService: MenuService,
    @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
    private settingService: SettingsService,
    private aclService: ACLService,
    private titleService: TitleService,
    @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
    private httpClient: HttpClient,
    private router: Router
  ) {
    iconSrv.addIcon(...ICONS_AUTO, ...ICONS);
  }

  private viaI18n(): Observable<void> {
    const defaultLang = this.i18n.defaultLang;
    return this.i18n.loadLangData(defaultLang).pipe(
      map((langData: NzSafeAny) => {
        this.i18n.use(defaultLang, langData);
      })
    );
  }

  private getCurrent() {
    return this.httpClient.get('api/index/menu').subscribe((res: any) => {
      if (res) {
        const appData = res.data;
        // Application data
        // Application information: including site name, description, year
        this.settingService.setApp(appData.app);
        // User information: including name, avatar, email address
        this.settingService.setUser(appData.user);
        // ACL: Set the permissions to full, https://ng-alain.com/acl/getting-started
        this.aclService.setFull(true);
        // Menu data, https://ng-alain.com/theme/menu
        this.menuService.add(appData.menu);
        // Can be set page suffix title, https://ng-alain.com/theme/title
        this.titleService.suffix = appData.app.name;
      }
    });
  }

  private viaMock(): Observable<void> {
    // const tokenData = this.tokenService.get();
    // if (!tokenData.token) {
    //   this.router.navigateByUrl(this.tokenService.login_url!);
    //   return;
    // }
    // mock
    const app: any = {
      name: `ng-alain`,
      description: `Ng-zorro admin panel front-end framework`
    };
    const user: any = {
      name: 'Admin',
      email: 'cipchk@qq.com',
      token: '123456789'
    };
    // Application information: including site name, description, year
    this.settingService.setApp(app);
    // User information: including name, avatar, email address
    // this.settingService.setUser(user);
    // ACL: Set the permissions to full, https://ng-alain.com/acl/getting-started
    this.aclService.setFull(true);
    // Menu data, https://ng-alain.com/theme/menu
    this.menuService.add([
      {
        text: '客户管理',
        group: true,
        children: [
          {
            text: '客户管理',
            link: '/system/user',
            icon: { type: 'icon', value: 'appstore' },
            children: [
              {
                text: '线索',
                link: '/system/user-clue',
                icon: { type: 'icon', value: 'appstore' }
              },
              {
                text: '客户',
                link: '/system/user',
                icon: { type: 'icon', value: 'appstore' }
              },
              {
                text: '公海',
                link: '/system/user-sea',
                icon: { type: 'icon', value: 'appstore' }
              }
            ]
          }
        ]
      },
      {
        text: '通讯录',
        group: true,
        children: [
          {
            text: '通讯录',
            link: '/system/book',
            icon: { type: 'icon', value: 'appstore' }
          }
        ]
      }
      // {
      //   text: '系统管理',
      //   group: true,
      //   children: [
      //     {
      //       text: '权限管理',
      //       link: '/dashboard',
      //       icon: { type: 'icon', value: 'appstore' }
      //     },
      //     {
      //       text: '角色管理',
      //       link: '/system/role',
      //       icon: { type: 'icon', value: 'appstore' }
      //     },
      //     {
      //       text: '部门管理',
      //       link: '/system/role-resource',
      //       icon: { type: 'icon', value: 'appstore' }
      //     },
      //     {
      //       text: '职位管理',
      //       link: '/system/role-resource',
      //       icon: { type: 'icon', value: 'appstore' }
      //     }
      //   ]
      // }
    ]);
    // Can be set page suffix title, https://ng-alain.com/theme/title
    this.titleService.suffix = app.name;

    return of();
  }

  load() {
    // http
    // return this.viaHttp();
    // mock: Don’t use it in a production environment. ViaMock is just to simulate some data to make the scaffolding work normally
    // mock：请勿在生产环境中这么使用，viaMock 单纯只是为了模拟一些数据使脚手架一开始能正常运行
    const tokenData = this.tokenService.get();
    if (tokenData && tokenData.token) {
      // this.getCurrent();
      this.viaMock();
    }
  }
}
